package com.msn.sign.util;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.text.Html;


public class SignSearch {
	
	private static String getString(InputStream is) {
        InputStreamReader r = null;
        try {
            StringBuilder sb = new StringBuilder();
            //TODO 这里是固定把网页内容的编码写在GBK,应该是可设置的
            r = new InputStreamReader(is, "UTF-8");
            char[] buffer = new char[128];
            int length = -1;
            while ((length = r.read(buffer)) != -1) {
                sb.append(new String(buffer, 0, length));
            }
            return sb.toString();
        } catch (Exception ex) {           
            return "";
        } finally {
            try {
                r.close();
            } catch (Exception ex) {
            	return "";
            }
        }
    }
	
	
	
	public static List<String> search(String date) throws IOException{
		String key = URLEncoder.encode(date, "UTF-8");
		URL url = new URL("http://show.msn.com.cn/TopNews.aspx?dt="+key);  
		//URL url = new URL(key);
		HttpURLConnection conn = (HttpURLConnection)url.openConnection();   
		conn.addRequestProperty("content-type","text/html;charset=UTF-8");
		conn.setRequestMethod("GET");  		
		InputStream in = conn.getInputStream();		 
		String ret = getString(in);
		
		in.close();		
//		ret = StringTool.htmlTrim2(ret);
		
		List<String> signList = new ArrayList<String>();
		Matcher m = Pattern.compile("(?<=<p>).*?(?=<br)",Pattern.DOTALL).matcher(ret);
        String content = null;
        int i=1;
        while (m.find()) {
        	content = m.group();
			content = content.replace("\r\n", "").replaceAll("^[　 ]+", "")
					.replaceAll("</?[^>]+>", "")
					.replaceAll("&quot;", "“");
			//        	content = i+":\n"+content;
        	signList.add(content);        	
        	i++;
        }
        return signList;
	}
	
	public static void main (String args[]){
		try {
			List<String> list = SignSearch.search("2007/07/25");
			System.out.println(list.size());
		} catch (IOException e) {			
			e.printStackTrace();
		}
	}

}
